<template>
  <div id="detail">
    <detail-nav-bar class="detail-nav" @titleClick="titleClick" :current-index="currentIndex"></detail-nav-bar>
    <scroll class="content" ref="scroll" @scroll="contentScroll" :probe-type="3">
      <detail-swiper :top-images="topImages"></detail-swiper>
      <detail-base-info :goods="goods"></detail-base-info>
      <detail-shop-info :shop="shop"></detail-shop-info>
      <detail-goods-info :detail-info="detailInfo" @imageLoad="imageLoad" />
      <detail-param-info :paramInfo="paramInfo" ref="param"></detail-param-info>
      <detail-comment-info :commentInfo="commentInfo" ref="comment"></detail-comment-info>
      <goods-list :goods="recommends" ref="recommend"></goods-list>
    </scroll>
    <detail-bottom-bar @addToCart="addToCart"></detail-bottom-bar>
    <back-top v-show="showBackTop" @click.native="backTop" />
  </div>
</template>

<script>
import DetailNavBar from './component/DetailNavBar'
import DetailSwiper from './component/DetailSwiper'
import DetailBaseInfo from './component/DetailBaseInfo.vue'
import DetailShopInfo from './component/DetailShopInfo.vue'
import DetailGoodsInfo from './component/DetailGoodsInfo'
import DetailParamInfo from './component/DetailParamInfo'
import DetailCommentInfo from './component/DetailCommentInfo.vue'
import DetailBottomBar from './component/DetailBottomBar.vue'

import Scroll from 'components/common/scroll/Scroll.vue'

import GoodsList from 'components/content/goods/GoodsList.vue'

import { getDetail, getRecommend, Goods, Shop, GoodsParam } from 'network/detail'
import { itemListenerMixin, backTopMixin } from 'common/mixin'

import { mapActions } from 'vuex'

export default {
  name: 'Detail',
  mixins: [itemListenerMixin, backTopMixin],
  components: {
    DetailNavBar,
    DetailSwiper,
    DetailBaseInfo,
    DetailShopInfo,
    DetailGoodsInfo,
    DetailParamInfo,
    DetailCommentInfo,
    DetailBottomBar,
    GoodsList,
    Scroll
  },
  data() {
    return {
      iid: '',
      topImages: [],
      goods: {},
      shop: {},
      detailInfo: {},
      paramInfo: {},
      commentInfo: {},
      recommends: [],
      themeTops: [],
      themeTops: [],
      currentIndex: 0
    }
  },
  created() {
    // 1.保存传入的iid
    this.iid = this.$route.params.iid
    // 2.根据iid请求详情数据
    getDetail(this.iid).then(res => {
      // 1.获取数据
      const data = res.result

      // 2.获取顶部的图片数据
      this.topImages = data.itemInfo.topImages

      // 3.获取商品信息
      this.goods = new Goods(data.itemInfo, data.columns, data.shopInfo.services)

      // 4.获取店铺信息
      this.shop = new Shop(data.shopInfo)

      // 5.获取商品详细信息
      this.detailInfo = data.detailInfo

      // 6.保存参数信息
      this.paramInfo = new GoodsParam(data.itemParams.info, data.itemParams.rule)

      // 7.保存评论数据
      if (data.rate.list) {
        this.commentInfo = data.rate.list[0]
      }
    })

    // 3.请求详情数据
    getRecommend().then(res => {
      this.recommends = res.data.list
    })
  },

  mounted() {
    // // 1.图片加载完成的事件监听
    // const refresh = debounce(this.$refs.scroll.refresh, 50)
    // this.$bus.$off('itemImageLoad').$on('itemImageLoad', () => {
    //   refresh()
    // })
  },
  methods: {
    ...mapActions(['addCart']),
    imageLoad() {
      this.$refs.scroll.refresh()
      // 获取对应的offsetTop
      this.themeTops = []
      this.themeTops.push(0)
      this.themeTops.push(this.$refs.param.$el.offsetTop)
      this.themeTops.push(this.$refs.comment.$el.offsetTop)
      this.themeTops.push(this.$refs.recommend.$el.offsetTop)
      this.themeTops.push(Number.MAX_VALUE)
    },
    titleClick(index) {
      this.$refs.scroll.scrollTo(0, -this.themeTops[index], 500)
    },
    contentScroll(position) {
      // 决定backTop按钮是否显示
      this.showBackTop = position.y <= -1000
      // 监听滚动到某个主题
      this._listenScrollTheme(-position.y)
    },
    _listenScrollTheme(position) {
      let length = this.themeTops.length
      for (let i = 0; i < length; i++) {
        let iPos = this.themeTops[i]
        /**
         * 判断的方案:
         *  方案一:
         *    条件: (i < (length-1) && currentPos >= iPos && currentPos < this.themeTops[i+1]) || (i === (length-1) && currentPos >= iPos),
         *    优点: 不需要引入其他的内容, 通过逻辑解决
         *    缺点: 判断条件过长, 并且不容易理解
         *  方案二:
         *    条件: 给themeTops最后添加一个很大的值, 用于和最后一个主题的top进行比较.
         *    优点: 简洁明了, 便于理解
         *    缺点: 需要引入一个较大的int数字
         * 疑惑: 在第一个判断中, 为什么不能直接判断(currentPos >= iPos)即可?
         * 解答: 比如在某一个currentPos大于第0个时, 就会break, 不会判断后面的i了.
         */
        if (this.currentIndex !== i && position >= iPos && position < this.themeTops[i + 1]) {
          this.currentIndex = i
        }
      }
    },
    addToCart() {
      const obj = {}
      obj.iid = this.iid
      obj.imgURL = this.topImages[0]
      obj.title = this.goods.title
      obj.desc = this.goods.desc
      obj.price = this.goods.realPrice
      // this.$store.dispatch('addCart', obj)
      this.addCart(obj).then(res => {
        this.$toast.show(res)
      })
    }
  }
}
</script>

<style lang="less" scoped>
#detail {
  height: 100vh;
  position: relative;
  z-index: 9;
  background-color: #fff;
  .content {
    // height: calc(100% - 44px);
    position: absolute;
    top: 44px;
    bottom: 58px;
    left: 0;
    right: 0;
    overflow: hidden;
  }
}
</style>
